fn main() {
    let ans = Solution::roman_to_int("III".to_string());
    println!("{}", ans);
}

struct Solution;

impl Solution {
    pub fn roman_to_int(s: String) -> i32 {
        let char_to_num = |c: char| -> i32 {
            match c {
                'I' => 1,
                'V' => 5,
                'X' => 10,
                'L' => 50,
                'C' => 100,
                'D' => 500,
                'M' => 1000,
                _   => panic!(),
            }
        };
        let mut ans = 0;
        let mut prev = 0;
        for c in s.chars().rev() {
            let num = char_to_num(c);
            if num >= prev {
                ans += num;
            } else {
                ans -= num;
            }
            prev = num;
        }
        ans
    }
}
